set(binary_testing_dir "${CDash_BINARY_DIR}/tests")
set(PHPUNIT "${CDash_SOURCE_DIR}/vendor/bin/phpunit")

# function to add a new PHP based coverage test to CDash
#
function(add_php_test TestName)
  add_test(
    NAME ${TestName}
    COMMAND ${PHP_EXE} ${testing_dir}/singletest.php ${testing_dir}/test_${TestName}.php
  )
  set_tests_properties(
    ${TestName} PROPERTIES
    FAIL_REGULAR_EXPRESSION ".*Failures: [1-9]+.*;.*Exceptions: [1-9]+.*"
  )
endfunction(add_php_test)
function(add_configured_php_test TestName)
  add_test(
    NAME ${TestName}
    COMMAND ${PHP_EXE} ${testing_dir}/singletest.php ${binary_testing_dir}/test_${TestName}.php
  )
  set_tests_properties(
    ${TestName} PROPERTIES
    FAIL_REGULAR_EXPRESSION ".*Failures: [1-9]+.*;.*Exceptions: [1-9]+.*"
  )
endfunction(add_configured_php_test)
function(add_unit_test TestName)
  add_test(
    NAME ${TestName}
    COMMAND ${PHP_EXE} ${phpunit_extra_arg} ${PHPUNIT} -c ${testing_dir}/phpunit.xml --bootstrap ${testing_dir}/bootstrap.php ${testing_dir}/case/${TestName}Test.php
  )
endfunction(add_unit_test TestName)

function(add_laravel_test TestName)
  add_test(
    NAME ${TestName}
    COMMAND ${PHP_EXE} ${phpunit_extra_arg} ${PHPUNIT} -c ${CDash_SOURCE_DIR}/phpunit.xml ${laravel_testing_dir}/${TestName}.php
  )
endfunction(add_laravel_test TestName)

# phpunit tests
set(phpunit_extra_arg "")
if (CDASH_CONFIGURE_HTACCESS_FILE)
    set(phpunit_extra_arg "-d auto_prepend_file=${CMAKE_BINARY_DIR}/prepend_coverage.php")
endif()

add_test(
  NAME php_style_check
  COMMAND ${CMAKE_SOURCE_DIR}/vendor/bin/php-cs-fixer fix --dry-run
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

add_php_test(install)
add_laravel_test(/Unit/app/Validators/PasswordTest)
add_laravel_test(/Feature/ConsumeSubmissionCommand)
add_laravel_test(/Feature/CDashTest)
add_laravel_test(/Feature/LdapAuthWithRulesTest)
add_laravel_test(/Feature/OpenLdapAuthWithOverrides)
add_laravel_test(/Feature/MigrateConfigCommand)
add_laravel_test(/Feature/PasswordRotation)
add_laravel_test(/Feature/ProjectPermissions)
add_laravel_test(/Feature/UserCommand)
add_unit_test(/PHPUnitTest)
add_unit_test(/CDash/Api/GitHubWebhook)
add_unit_test(/CDash/BuildUseCase)
add_unit_test(/CDash/Config)
add_unit_test(/CDash/ConfigUseCase)
add_unit_test(/CDash/Controller/Auth/Session)
add_unit_test(/CDash/Database)
add_unit_test(/CDash/Lib/Repository/GitHub)
add_unit_test(/CDash/LinkifyCompilerOutput)
add_unit_test(/CDash/Log)
add_unit_test(/CDash/Messaging/Subscription/CommitAuthorSubscriptionBuilder)
add_unit_test(/CDash/Messaging/Subscription/UserSubscriptionBuilder)
add_unit_test(/CDash/Messaging/Topic/AuthoredTopic)
add_unit_test(/CDash/Messaging/Topic/BuildErrorTopic)
add_unit_test(/CDash/Messaging/Topic/ConfigureTopic)
add_unit_test(/CDash/Messaging/Topic/DynamicAnalysisTopic)
add_unit_test(/CDash/Messaging/Topic/EmailSentTopic)
add_unit_test(/CDash/Messaging/Topic/FixedTopic)
add_unit_test(/CDash/Messaging/Topic/MissingTestTopic)
add_unit_test(/CDash/Messaging/Topic/TestFailureTopic)
add_unit_test(/CDash/Messaging/Topic/TopicDecorator)
add_unit_test(/CDash/Messaging/Topic/UpdateErrorTopic)
add_unit_test(/CDash/Middleware/OAuth2)
add_unit_test(/CDash/Middleware/OAuth2/GitHub)
add_unit_test(/CDash/Middleware/OAuth2/GitLab)
add_unit_test(/CDash/Middleware/OAuth2/Google)
add_unit_test(/CDash/Middleware/Queue)
add_unit_test(/CDash/Middleware/Queue/DriverFactory)
add_unit_test(/CDash/Middleware/Queue/Initializer)
add_unit_test(/CDash/Middleware/Queue/SubmissionService)
add_unit_test(/CDash/Model/BuildError)
add_unit_test(/CDash/Model/BuildErrorFilter)
add_unit_test(/CDash/Model/BuildFailure)
add_unit_test(/CDash/Model/BuildRelationship)
add_unit_test(/CDash/Model/Repository)
add_unit_test(/CDash/MultipleSubprojectsEmail)
add_unit_test(/CDash/NightlyTime)
add_unit_test(/CDash/Service/RepositoryService)
add_unit_test(/CDash/ServiceContainer)
add_unit_test(/CDash/Submission/CommitAuthorHandlerTrait)
add_unit_test(/CDash/TestUseCase)
add_unit_test(/CDash/UpdateUseCase)
add_unit_test(/CDash/XmlHandler/BuildHandler)
add_unit_test(/CDash/XmlHandler/ConfigureHandler)
add_unit_test(/CDash/XmlHandler/DynamicAnalysisHandler)
add_unit_test(/CDash/XmlHandler/TestingHandler)
add_unit_test(/CDash/XmlHandler/UpdateHandler)

add_laravel_test(/Feature/TestSchemaMigration)
add_laravel_test(/Feature/MeasurementPositionMigration)
add_laravel_test(/Feature/RemoveMeasurementCheckboxesMigration)
add_laravel_test(/Feature/IncreaseSiteInformationCPUColumnsSizeMigration)
add_php_test(install_into_empty_db)
add_php_test(reinstall)
add_php_test(compressedtest)
add_php_test(createpublicdashboard)
add_php_test(email)
add_php_test(projectwebpage)
add_php_test(subproject)
add_php_test(actualtrilinossubmission)
add_php_test(summaryemail)

add_php_test(upgrade)
add_php_test(aggregatecoverage)
add_php_test(banner)
add_php_test(buildconfigure)
add_php_test(builderrordiff)
add_php_test(buildgroupposition)
add_php_test(buildgrouprule)
add_php_test(buildoverview)
add_php_test(buildusernote)
add_php_test(committerinfo)
add_php_test(dailyupdatefile)
add_php_test(edituser)
add_php_test(image)
add_php_test(import)
add_php_test(importbackup)
add_php_test(importbuilds)
add_php_test(managebanner)
add_php_test(managebackup)
add_php_test(manageprojectroles)
add_php_test(manageusers)
#add_php_test(processsubmissions)
add_php_test(projectindb)
add_php_test(pubproject)
add_php_test(projectmodel)
add_php_test(querytests)
add_php_test(sitestatistics)
add_php_test(gitinfo)
add_php_test(testenv)
add_php_test(testoverview)
add_php_test(userstatistics)
add_php_test(user)
add_php_test(viewconfigure)
add_php_test(viewdynamicanalysis)
add_php_test(viewdynamicanalysisfile)
add_php_test(viewmap)

add_php_test(viewsubprojectdependencies)
add_php_test(buildmodel)
add_php_test(sitemodel)
add_unit_test(/CDash/Model/PendingSubmissions)
add_php_test(projectxmlsequence)
add_php_test(uploadfile)
add_php_test(branchcoverage)
add_php_test(multicoverage)
add_php_test(javajsoncoverage)
add_php_test(jscovercoverage)
add_php_test(opencovercoverage)
add_php_test(buildfailuredetails)
add_php_test(builddetails)
add_php_test(updateappend)
add_php_test(notesapi)
add_php_test(hidecolumns)
add_php_test(subprojectnextprevious)
add_php_test(excludesubprojects)
add_php_test(testhistory)
add_php_test(expectedandmissing)
add_php_test(externallinksfromtests)
add_php_test(timesummary)
add_php_test(buildgetdate)
add_php_test(replacebuild)
add_php_test(sequenceindependence)
add_php_test(passwordcomplexity)
add_php_test(crosssubprojectcoverage)
add_php_test(aggregatesubprojectcoverage)
add_php_test(configurewarnings)
add_php_test(filtertestlabels)
add_php_test(seconds_from_interval)
add_php_test(dynamicanalysissummary)
add_php_test(viewsubprojects)
add_php_test(truncateoutput)
add_php_test(csvexport)
add_php_test(uniquediffs)
add_php_test(imagecomparison)
add_php_test(createprojectpermissions)
add_php_test(testgraphpermissions)
add_php_test(extracttar)
add_php_test(pdoexecutelogserrors)
add_php_test(revisionfilteracrossdates)
add_php_test(timeoutsandmissingtests)
add_php_test(disabledtests)
add_php_test(multiplesubprojects)
add_php_test(authtoken)
add_php_test(junithandler)
add_php_test(issuecreation)
add_php_test(limitedbuilds)
add_php_test(managemeasurements)
add_php_test(subprojectemail)
add_php_test(coveragedirectories)
add_php_test(outputcolor)
add_php_test(buildproperties)
add_php_test(timestatus)
add_php_test(bazeljson)
add_php_test(filterbuilderrors)
add_php_test(buildrelationship)
add_php_test(submission_assign_buildid)
add_php_test(donehandler)
add_php_test(changeid)
add_php_test(updateonlyuserstats)
add_php_test(expiredbuildrules)
add_php_test(filterblocks)
add_php_test(indexnextprevious)
add_php_test(putdynamicbuilds)
add_php_test(commitauthornotification)
add_php_test(addbuildapi)
add_php_test(subscribeprojectshowlabels)
add_php_test(rehashpassword)
add_php_test(consistenttestingday)
add_php_test(numericupdate)
add_php_test(attachedfiles)
add_php_test(subprojectorder)
add_php_test(testimages)
add_php_test(dynamicanalysislogs)
add_php_test(namedmeasurements)
add_php_test(longbuildname)
add_php_test(multiplelabelsfortests)
add_php_test(subprojecttestfilters)
add_php_test(dynamicanalysisdefectlongtype)
add_php_test(starttimefromnotes)
add_php_test(querytestsfilterlabels)
add_php_test(lotsofsubprojects)
add_php_test(querytestsrevisionfilter)
add_php_test(redundanttests)
add_php_test(configureappend)
add_php_test(notesparsererrormessages)

add_subdirectory(ctest)

add_subdirectory(js/e2e_tests)

if(CDASH_USE_SELENIUM)
  file(MAKE_DIRECTORY ${CDash_BINARY_DIR}/cmake.git)
  add_subdirectory(selenium)
endif()

# These should go last so the removal of builds doesn't clobber other tests
add_subdirectory(autoremovebuilds)
add_laravel_test(/Feature/AutoRemoveBuildsCommand)

# Slow tests that need more time in CI.
set_tests_properties(
  actualtrilinossubmission
  PROPERTIES TIMEOUT 1800)
